<template>
	<view class="container">
		<view class="avatar">
			<!-- 上传组件 -->
			<u-upload name="product_img" :maxCount="1" :fileList="filelist" width="100" height="100" @delete="del"
				@afterRead="read" />
		</view>


		<view class="content">
			<u-form labelPosition="left" labelWidth="60" :model="insurelist" :rules="rules" ref="insure">
				<u-form-item label="保险名:" prop="name">
					<u-input v-model="insurelist.name" ref="name" />
				</u-form-item>
				<!-- 联系人 -->
				<u-form-item label="门店" prop="store.name" borderBottom @click="showstore = true">
					<u-input v-model="insurelist.store.name" ref="store.name" disabledColor="#ffffff" placeholder="请选择所属门店"
						border="none"></u-input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>

				<!-- 商品描述 -->
				<u-form-item label="内容1:" prop="content1" borderBottom>
					<u-textarea v-model="insurelist.content1" ref="content1" count></u-textarea>
				</u-form-item>
				<u-form-item label="内容2:" prop="content2" borderBottom>
					<u-textarea v-model="insurelist.content2" ref="content2" count></u-textarea>
				</u-form-item>
				<u-form-item label="提示:" prop="tips" borderBottom>
					<u-textarea v-model="insurelist.tips" ref="tips" count></u-textarea>
				</u-form-item>

				<!-- 参数3 -->
				<!-- <u-form-item label="创建时间:">
					<u-input v-model="insurelist.createtime_text" disabled />
				</u-form-item> -->


				<!-- 全款 -->
				<u-form-item label="价格:" prop="free">
					<u-input  v-model="insurelist.free" ref="free" />
				</u-form-item>

				<u-action-sheet :show="showstore" :actions="storelist" title="请选所属的门店" @close="showstore = false"
					@select="storeSelect()">
				</u-action-sheet>




				<!-- 提交按钮 -->
				<u-button text="开始修改" v-if="this.insureid != 0" icon="pushpin" size="small" type="success" @click="edit" />
			</u-form>

		</view>
		<!-- 消息提醒 -->
		<u-toast ref="notice"></u-toast>
	</view>
</template>
  
<script>
export default {
	onLoad(options) {

		this.insureid = options.insureid ? options.insureid : 0

		this.auth = this.$u.auth.check();
		this.info()
		this.storedata()

	},


	created() {

		this.timer = setInterval(() => {

			let a = this.$u.count.check()

		}, 15000);

	},
	onReady() {
		this.$refs.insure.setRules(this.rules)

	},
	beforeDestroy() {
		clearInterval(this.timer)
	},
	data() {
		return {
			filelist: [],
			insureid: 0,
			auth: {},
			insurelist: {
				// 一定要初始化一级结构里面的数据，不然会报错
				store: {
					name: ""
				}
			},
			showstore: false,
			storelist: [],
			rules: {

				name: {
					required: true,
					message: "请填写保险名称",
					trigger: ["blur", "change"],
				},
				content1: {
					required: true,
					message: "不低于3个字",
					trigger: ["change"],
				},
				content2: {
					required: true,
					message: "不低于3个字",
					trigger: ["change"],
				},
				tips: {
					required: true,
					message: "不低于3个字",
					trigger: ["change"],
				},
				"store.name": {
					required: true,
					message: "请选择你所属的门店",
					trigger: ["blur", "change"],
				},
				free: {
					required: true,
					message: "请填写保险价格",
					trigger: ["blur", "change"],
				},


			},




		};
	},

	methods: {


		async read(event) {

			//组装上传数据
			var data = {
				filePath: event.file.thumb,
				name: "product_img",
				header: {},

			};

			//调用接口
			var result = await this.$u.api.product.thumbs(data);

			if (result.code) {

				this.insurelist.thumb_text = result.data
				// 合并对象
				this.filelist = [
					{ url: result.data }
				]
				return false


			} else {
				this.$refs.notice.show({
					type: "error",
					message: result.msg,
				});
				return false;
			}
		},

		async del(e) {

			let data = {
				adminid: this.auth.id,
				insureid: this.insureid,
				url: e.file.url
			}


			var result = await this.$u.api.hire.delinsureimg(data)

			if (result.code) {
				this.$refs.notice.show({
					type: "success",
					message: result.msg,
					duration: 1000,
					complete: () => {

						if (e.index == 0) {
							this.filelist.splice(e.index, e.index)

							this.filelist = []
						}
					}
				})

			} else {
				this.$refs.notice.show({
					type: "danger",
					message: result.msg,
					duration: 1000,

				}
				)
			}



		},


		storeSelect(e) {
			this.insurelist.store.name = e.name;
			this.insurelist.stoid = e.id;
			this.$refs.insure.validateField('store.name')


		},
	
		async info() {
			let data = {
				insureid: this.insureid,
				adminid: this.auth.id
			}
			let result = await this.$u.api.hire.insureinfo(data)

			if (result.code) {
				this.insurelist = result.data
				if (this.insurelist.thumb_text) {

					this.filelist = [
						{ url: this.insurelist.thumb_text }

					]
				}


			} else {
				this.$refs.notice.show({
					type: "danger",
					message: result.msg,
					duration: 2000
				})
			}
		},
		async storedata() {

			let data = {
				adminid: this.auth.id,
				stoid: this.auth.stoid
			}
			let result = await this.$u.api.hire.storelist(data);

			if (result.code) {
				this.storelist = result.data;
			} else {
				this.$refs.notice.show({
					type: "danger",
					message: result.msg,
				});
			}
		},
		edit() {

			this.$refs.insure.validate().then(async () => {
				let data = {
					adminid: this.auth.id,
					id: this.insureid,
					content1: this.insurelist.content1,
					content2: this.insurelist.content2,
					tips: this.insurelist.tips,
					free: this.insurelist.free,
					stoid: this.insurelist.stoid,
					createtime: this.insurelist.createtime_text,
					name: this.insurelist.name,
					thumb:this.insurelist.thumb_text

				}
				uni.request({
					url: 'https://luxuries.h1668.com/first/hire/hire/insureedit', //仅为示例，并非真实接口地址。
					data: data,
					method: "POST",
					header: {
						'X-Requested-With': 'XMLHttpRequest'
					},
					success: (result) => {
						if (result.data.code) {
							this.$refs.notice.show({
								type: "success",
								message: result.data.msg,
								duration: 1000,
								complete: () => {
									uni.$u.route({
										type: "reLaunch",
										url: "/pagesC/product/insure/edit",
										params: {
											insureid: this.insureid
										}
									})
								}
							});
						}else
						{
							this.$refs.notice.show({
								type:"error",
								message:result.data.msg,
								duration:1500

							})
						}
					}
				})


			}).catch(() => {
				console.log(564)
				uni.$u.toast("效验失败");
			});



		}



	},
};
</script>
  
<style >
.content {
	padding: 20px;
}

.container {
	height: 100%;
	margin: 0 50rpx;
	box-sizing: border-box;
}

.avatar {
	text-align: center;
}

.u-avatar {
	margin: 80rpx auto 40rpx;
}

.xcx {
	width: 200rpx;
	height: 200rpx;
	margin: 80rpx auto 40rpx;
	border-radius: 100%;
	overflow: hidden;
}

.nickname {
	font-size: 14px;
	text-align: center;
}

.title {
	text-align: center;
	margin: 40rpx 0;
	font-size: 14rpx;
}

.avatar .u-upload {
	justify-content: center;
	align-items: center;
	margin: 3em auto;
}
</style>
  